import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangweiqiang
 * @version 1.0
 * @date 2021-08-16 0:14
 */
public class ActivitiDemo {
    @Test
    public void testDeployment() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/evection.bpmn") // 添加bpmn资源
                .addClasspathResource("bpmn/evection.png")  // 添加png资源
                .name("出差2")
                .disableSchemaValidation()//禁用验证
                .deploy();

        System.out.println("流程ID：" + deployment.getId());
        System.out.println("流程名称：" + deployment.getName());
    }

    @Test
    public void testStartProcess() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();


        //ProcessInstance processInstance = runtimeService.startProcessInstanceById("evection:2:20004");

        final ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("evection");


        System.out.println("流程部署id：" + processInstance.getId());
        System.out.println("流程定义id：" + processInstance.getProcessDefinitionId());
        System.out.println("当前活动id：" + processInstance.getActivityId());
    }

    @Test
    public void testFindPersonalTaskList() {
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();

        final List<Task> tasks = taskService.createTaskQuery()
                .processDefinitionKey("evection")//查询流程key为evection的任务
                .taskAssignee("zhangsan")//查询负责人为张三的的任务
                .list();

        for (Task task : tasks) {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
            System.out.println("===================================================");
        }
    }

    @Test
    public void completeTask() {
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();


        taskService.complete("32505");
    }


    @Test
    public void testQueryProcessDefInfo() {
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final RepositoryService repositoryService = processEngine.getRepositoryService();
        final ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();

        final List<ProcessDefinition> list = query
                .processDefinitionKey("evection")   //条件：processDefinitionKey =evection
                .orderByProcessDefinitionVersion()//orderByProcessDefinitionVersion 按照版本排序
                .desc()
                .list();

        for (ProcessDefinition processDefinition : list) {
            System.out.println("流程定义 id="+processDefinition.getId());
            System.out.println("流程定义 name="+processDefinition.getName());
            System.out.println("流程定义 key="+processDefinition.getKey());
            System.out.println("流程定义 Version="+processDefinition.getVersion());
            System.out.println("流程部署 ID="+processDefinition.getDeploymentId());
            System.out.println("===================================================");
        }
    }


    @Test
    public void testDeleteProcess(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.deleteDeployment("5001",true);
    }

    @Test
    public void testDownloadSource() throws IOException {
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final RepositoryService repositoryService = processEngine.getRepositoryService();
        //获取key为evection的流程定义
        final ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().processDefinitionId("evection:4:27504").singleResult();

        final String deploymentId = definition.getDeploymentId();
        final InputStream diagram = repositoryService.getResourceAsStream(deploymentId, definition.getDiagramResourceName());
        final InputStream bpmn = repositoryService.getResourceAsStream(deploymentId, definition.getResourceName());

        //构造输出流
        File file_png = new File("F:\\evectionflow01.png");
        File file_bpmn = new File("F:\\evectionflow01.bpmn");
        FileOutputStream bpmnOut = new FileOutputStream(file_bpmn);
        FileOutputStream pngOut = new FileOutputStream(file_png);

        IOUtils.copy(diagram,pngOut);
        IOUtils.copy(bpmn,bpmnOut);


    }


    @Test
    public void testHistory(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        final HistoryService historyService = processEngine.getHistoryService();

        //获取actinst查询对象
        final HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery();

        //根据流程的DefinitionId查询
        //instanceQuery.processDefinitionId("evection:2:20004");

        //根据instanceId查询
        instanceQuery.processInstanceId("32501");

        //根据创建时间降序排列
        instanceQuery.orderByHistoricActivityInstanceStartTime().desc();

        final List<HistoricActivityInstance> list = instanceQuery.list();

        for (HistoricActivityInstance hi : list) {
            System.out.println(hi.getActivityId());
            System.out.println(hi.getActivityName());
            System.out.println(hi.getProcessDefinitionId());
            System.out.println(hi.getProcessInstanceId());
            System.out.println("<==========================>");
        }
    }
}
